import router from "@/router";
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import pinia from "@/store";
import useUserStore from '@/store/modules/user'
const userStore = useUserStore(pinia)

router.beforeEach((to, from, next) => {
  NProgress.start()
  if (userStore.token && to.path === '/login') {
    next('/home')
    return
  } else if (!userStore.token && to.path !== '/login') {
    next({
      path: '/login',
      query: {
        redirect: to.path
      }
    })
    return
  }
  next()
})

router.afterEach((to, from, next) => {
  NProgress.done(); // 完成进度条
})